﻿using Canteen.Core.Models;
using Canteen.Core.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Canteen.Core.WebApp.API.Controllers
{
    [Route("admin/store")]
    [ApiController, Authorize(Roles = "admin")]
    public class AdminStoreController : ControllerBase
    {
        #region 构造函数
        private readonly IStoreService storeService;

        public AdminStoreController(IStoreService storeService)
        {
            this.storeService = storeService;
        }
        #endregion

        // GET: api/<StoreController>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<StoreController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST store/login
        [HttpPost("login"), AllowAnonymous]
        public async Task<HttpResult<object>> PostLogin([FromBody] StoreViewModel obj)
        {
            StoreLoginViewModel login= new StoreLoginViewModel();
            Store store;
            //超级管理员登录
            if (obj.Phone == "admin" && obj.PassWord == "qwerty@123456")
            {
                login.Id = 0;
                login.Name = "超级管理员";
                login.Leader = "admin";
                store = new Store() { Id = 0, Name = login.Name, Leader = login.Leader };
            }
            else
            {
                store = await storeService.SelectStoreByPhoneAndPwd(obj.Phone, obj.PassWord);
                if (store is null) throw new Exception("账号或密码错误");
                else if (store.Verify != 1) throw new Exception("商家认证审核失败");
                else if (store.Status != 1) throw new Exception("商家经营状态异常");
                login.Id = store.Id;
                login.Name = store.Name;
                login.Leader = store.Leader;
            }

            var jwtAuthResult = await storeService.SetStoreLogin(store);
            if (jwtAuthResult is null) throw new Exception("设置登录失败");
            //6、返回结果
            return Result.Success(new
            {
                jwtAuthResult.AccessToken,
                jwtAuthResult.RefreshToken,
                store = login
            });
        }

        // POST store/register
        [HttpPost("register"), AllowAnonymous]
        public async Task<HttpResult<int>> PostRegister([FromBody] Store obj)
        {
            if(await storeService.ExistStoreByRegister(obj)) throw new Exception(obj.Name +"已经注册");
            var store = await storeService.CreateStore(obj);
           
            return HttpResult<int>.Success(store.Status);
        }


        [HttpGet("PageList")]
        public async Task<HttpResult<PageList<Store>>> GetPageList([FromQuery] StorePageParameters parameters)
        {
            return HttpResult<PageList<Store>>.Success(await storeService.GetPageList(parameters));
        }
        // PUT store/changestatus/5
        [HttpPut("ChangeStatus/{id}")]
        public async Task<HttpResult<string>> ChangeStatus(int id, [FromQuery] int status)
        {
            if (await storeService.ChangeStoreStatus(id, status)) { 
                return HttpResult<string>.Success("修改成功");
            }
            throw new Exception("修改失败");
        }
        // PUT store/changeverify/5
        [HttpPut("ChangeVerify/{id}")]
        public async Task<HttpResult<string>> ChangeVerify(int id, [FromQuery] int verify)
        {
            if (await storeService.ChangeStoreVerify(id, verify))
            {
                return HttpResult<string>.Success("修改成功");
            }
            throw new Exception("修改失败");
        }

        // DELETE api/<StoreController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
